//
//  OADGroupedTableInfoViewController.m
//  OASDK_Works
//
//  Created by ChenJie on 4/7/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADGroupedTableInfoViewController.h"
#import "OASDK.h"

@interface OADGroupedTableInfoViewController ()
<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic, strong) NSMutableArray *cells;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation OADGroupedTableInfoViewController

#pragma mark - ViewController lifecycle

- (instancetype)init {
    self = [super init];
    if (self) {
        [self buildDataSource];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSStringFromClass(self.class);
    
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGFloat topMark = 0;
    CGSize viewSize = self.view.bounds.size;
    
    self.tableView.frame = CGRectMake(0, topMark, viewSize.width, viewSize.height - topMark);
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - Public methods



#pragma mark - Private methods

- (void)buildDataSource {
    NSArray *cells = @[
        @{@"class": UITableViewCell.class,
          @"title": @"One",
          @"target": UIViewController.class,
        },
        @{@"class": UITableViewCell.class,
          @"title": @"Two",
          @"target": @"UIViewController",
        },
        @{@"title": @"Three",
        },
        @{@"class": UITableViewCell.class,
          @"title": @"Four",
          @"target": UIViewController.class,
        },
    ];
    
    [self.cells addObjectsFromArray:cells];
}

- (Class)cellClassAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *cellInfo = self.cells[indexPath.row];
    Class cellClass = cellInfo[@"class"];
    return cellClass ?: UITableViewCell.class;
}

- (NSString *)titleAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *cellInfo = self.cells[indexPath.row];
    return cellInfo[@"title"];
}

- (UIViewController *)targetViewControllerAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *cellInfo = self.cells[indexPath.row];
    Class targetVcClass = cellInfo[@"target"] ?: UIViewController.class;
    if ([targetVcClass isKindOfClass:NSString.class]) {
        targetVcClass = NSClassFromString((NSString *)targetVcClass);
    }
    
    UIViewController *targetVc = [[targetVcClass alloc] init];
    if ([targetVc isMemberOfClass:UIViewController.class]) {
        targetVc.view.backgroundColor = [UIColor whiteColor];
        targetVc.navigationItem.title = NSStringFromClass(targetVc.class);
    }
    
    return targetVc;
}

#pragma mark - Event response methods

- (void)buttonClicked:(UIButton *)button {
    NSLog(@"button clicked...");
}

#pragma mark - Deletate methods



#pragma mark - <UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cells.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

#pragma mark - <UITableViewDelegate>

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Class cellClass = [self cellClassAtIndexPath:indexPath];
    NSString *cellIdentifier = NSStringFromClass(cellClass);
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    cell.textLabel.text = [self titleAtIndexPath:indexPath];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UIViewController *vc = [self targetViewControllerAtIndexPath:indexPath];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Setter



#pragma mark - Getter

- (NSMutableArray *)cells {
    if (!_cells) {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:3];
        _cells = array;
    }
    return _cells;
}

- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        tableView.dataSource = self;
        tableView.delegate = self;
//        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//
//        tableView.estimatedRowHeight = 0;
//        tableView.estimatedSectionHeaderHeight = 0;
//        tableView.estimatedSectionFooterHeight = 0;
//
//        tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
//        tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
//
//        tableView.tableHeaderView.backgroundColor = OARandomColor();
//        tableView.tableFooterView.backgroundColor = OARandomColor();
        
        
        //////////
        
//                tableView.backgroundColor = OAColor(efefef);
//                tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//                tableView.estimatedRowHeight = 0;
//
//        //        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 0.001)];
//                tableView.tableHeaderView = [UIView new];
//
//        //        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 0.001)];
//                tableView.tableFooterView = [UIView new];
//
//                tableView.tableHeaderView.frame = CGRectMake(0, 0, 375, 1);
//                tableView.tableHeaderView.backgroundColor = OARandomColor();
//
//                tableView.tableFooterView.frame = CGRectMake(0, 0, 375, 1);
//                tableView.tableFooterView.backgroundColor = OARandomColor();
        
        
        ////
        
//        tableView.estimatedRowHeight = 0;
//        tableView.estimatedSectionHeaderHeight = 0;
//        tableView.estimatedSectionFooterHeight = 0;
//        tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.001)];
//        tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.001)];
//        tableView.tableHeaderView.backgroundColor = OARandomColor();
//        tableView.tableFooterView.backgroundColor = OARandomColor();
        
        tableView.backgroundColor = OAColor(efefef);
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.estimatedRowHeight = 0;
        
//        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 0.001)];
        tableView.tableHeaderView = [UIView new];
        
//        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 0.001)];
        tableView.tableFooterView = [UIView new];
        
        tableView.tableHeaderView.frame = CGRectMake(0, 0, 375, 1);
        tableView.tableHeaderView.backgroundColor = OARandomColor();
        
        tableView.tableFooterView.frame = CGRectMake(0, 0, 375, 1);
        tableView.tableFooterView.backgroundColor = OARandomColor();
        
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - Memory management

- (void)dealloc {
}

@end
